package com.lmy.mallback.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmy.mallback.entity.Comment;
import com.lmy.mallback.entity.Product;
import com.lmy.mallback.entity.ProductSwiperImage;
import com.lmy.mallback.service.CommentService;
import com.lmy.mallback.service.ProductService;
import com.lmy.mallback.service.ProductSwiperImageService;
import com.lmy.mallback.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 程序员小洋
 * @version 2022.3
 * @date 2023/1/11 11:48
 * @title 商品controller层
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSwiperImageService productSwiperImageService;

    @Autowired
    private CommentService commentService;

    /**
     * 查询轮播商品
     * 思路:
     * 查询isSwiper为true的商品 true说明该商品轮播
     * 按照swiperSort排序 商品轮播的顺序
     * @return
     */
    @GetMapping("/findSwiper")
    public R findSwiper() {
        List<Product> swiperProductList = productService.list(new QueryWrapper<Product>()
                .eq("isSwiper", true).orderByAsc("swiperSort"));
        Map<String,Object> map = new HashMap<>();
        map.put("message",swiperProductList);
        return R.ok(map);
    }

    /**
     * 查询热门推荐商品
     * 思路:
     * 查询isHot为true的商品 true说明商品为热门推荐
     * 按照hotDateTime时间排序 取前8个
     * @return
     */
    @GetMapping("/findHot")
    public R findHot(){
        Page<Product> page = productService.page(new Page<>(0,8),new QueryWrapper<Product>()
                .eq("isHot",true).orderByAsc("hotDateTime"));
        List<Product> hotProductList = page.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("message",hotProductList);
        return R.ok(map);
    }

    /**
     * 根据id查询商品信息
     * 思路:
     * 1.根据商品图片表中的外键productId跟商品表的id相等来判断是哪一个商品的图片
     * 2.按照商品图片表中的sort字段进行升序排序
     * 3.将商品图片表中的信息设置到商品的ProductSwiperImageList中
     * 4.返回给前端
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public R detail(Integer id){
        Product product = productService.getById(id);
        List<ProductSwiperImage> productSwiperImageList = productSwiperImageService.list(new QueryWrapper<ProductSwiperImage>()
                .eq("productId", product.getId()).orderByAsc("sort"));
        product.setProductSwiperImageList(productSwiperImageList);
        Map<String,Object> map = new HashMap<>();
        map.put("message",product);
        return R.ok(map);
    }

    @GetMapping("/comment")
    public R comment(Integer id){
        Product product = productService.getById(id);
        List<Comment> productSwiperImageList = commentService.list(new QueryWrapper<Comment>()
                .eq("productId", product.getId()).orderByAsc("sort"));
        product.setCommentList(productSwiperImageList);
        Map<String,Object> map = new HashMap<>();
        map.put("message",product);
        return R.ok(map);
    }

    @PostMapping("/saveComment")
    public R saveComment(@RequestBody Comment comment){
        boolean save = commentService.save(comment);
        Map<String,Object> map = new HashMap<>();
        map.put("message",comment);
        return R.ok(map);
    }

    /**
     * 商品搜索
     * @param q
     * @return
     */
    @GetMapping("/search")
    public R search(String q){
        List<Product> productList = productService.list(new QueryWrapper<Product>().like("name", q));
        Map<String,Object> map=new HashMap<>();
        map.put("productList",productList);
        return R.ok(map);
    }
}
